package com.haina.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haina.shop.context.BaseContext;
import com.haina.shop.domain.UserCollection;
import com.haina.shop.service.UserCollectionService;
import com.haina.shop.mapper.UserCollectionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import javax.annotation.Resource;

/**
* @author 48077
* @description 针对表【tz_user_collection】的数据库操作Service实现
* @createDate 2024-07-31 18:34:30
*/
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection>
    implements UserCollectionService{

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Override
    public int  collection(Long prodId) {
        Long userId = BaseContext.getCurrentId();

        int exists = userCollectionMapper.isExists(prodId,userId);

        return exists;
    }

    @Override
    public void addOrCancel(Long prodId) {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        int exist = userCollectionMapper.isExists(prodId, userId);
        if (exist > 0) {
            userCollectionMapper.deleteByProdId(prodId, userId);
        } else {
            UserCollection userCollection = new UserCollection();
            userCollection.setUserId(String.valueOf(userId));
            userCollection.setCreateTime(new Date());
            userCollection.setProdId(prodId);
            userCollectionMapper.insert(userCollection);
        }
    }

    public long selectByUserId(String userId){
        long l = userCollectionMapper.selectByCount(userId);
        return l;
    }

}




